package com.hualude.roadsign.utils;

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.RichTextString;

import java.util.Calendar;
import java.util.Date;

/**
 * Created by yang_jiaren on 2017/5/18.
 */
public class ExportExcelUtil {
    private HSSFWorkbook hssfWorkbook;

    public HSSFWorkbook getHssfWorkbook() {
        return hssfWorkbook;
    }

    public void setHssfWorkbook(HSSFWorkbook hssfWorkbook) {
        this.hssfWorkbook = hssfWorkbook;
    }

    public ExportExcelUtil() {
        hssfWorkbook = new HSSFWorkbook();
    }

    public ExportExcelUtil(HSSFWorkbook hssfWorkbook) {
        this.hssfWorkbook = hssfWorkbook;
    }

    //excel 创建字体时详细的信息
    public HSSFFont createExFont(String fontName, int size, int weight) {
        HSSFFont font = hssfWorkbook.createFont();
        font.setFontName(fontName);
        font.setFontHeightInPoints((short) size);
        font.setBoldweight((short) weight);
        return font;
    }

    //excel 创建样式时详细的信息
    public HSSFCellStyle createExStyle(short Alignment, short verticalAlignment, HSSFFont font) {
        HSSFCellStyle style = hssfWorkbook.createCellStyle();
        style.setAlignment(Alignment);
        style.setVerticalAlignment(verticalAlignment);
        style.setFont(font);
        return style;
    }

    //excel 创建样式时顺带字体一起创建
    public HSSFCellStyle createExStyle(String fontName, int size, int weight, short Alignment, short verticalAlignment) {
        HSSFCellStyle style = hssfWorkbook.createCellStyle();
        HSSFFont font = hssfWorkbook.createFont();
        font.setFontName(fontName);
        font.setFontHeightInPoints((short) size);
        font.setBoldweight((short) weight);
        style.setAlignment(Alignment);
        style.setVerticalAlignment(verticalAlignment);
        style.setFont(font);
        return style;
    }

    //excel 创建sheet时详细的信息
    public HSSFSheet createExSheet(String sheetName, int rowHeight, int colWidth) {
        HSSFSheet sheet = hssfWorkbook.createSheet(sheetName);
        sheet.setDefaultRowHeight((short) rowHeight);
        sheet.setDefaultColumnWidth(colWidth);
        return sheet;
    }

    //创建表头 接收的是一个表头信息String数组 每列会按数组里面的数据顺序放置
    //rowLocation 表头所在的行 0开始
    public void createExSheetHeader(HSSFSheet sheet, HSSFCellStyle style, int rowLocation, String[] headerInfo) {
        HSSFRow row = sheet.createRow(rowLocation);
        for (int i = 0; i < headerInfo.length; i++) {
            HSSFCell info = row.createCell(i);
            info.setCellStyle(style);
            info.setCellValue(headerInfo[i]);
        }
    }

    //表格里面的信息
    public void addExInfo(HSSFRow row, int column, HSSFCellStyle style, double value) {
        HSSFCell bodyInfo = row.createCell(column);
        bodyInfo.setCellStyle(style);
        bodyInfo.setCellValue(value);
    }

    public void addExInfo(HSSFRow row, int column, HSSFCellStyle style, Date value) {
        HSSFCell bodyInfo = row.createCell(column);
        bodyInfo.setCellStyle(style);
        bodyInfo.setCellValue(value);
    }

    public void addExInfo(HSSFRow row, int column, HSSFCellStyle style, Calendar value) {
        HSSFCell bodyInfo = row.createCell(column);
        bodyInfo.setCellStyle(style);
        bodyInfo.setCellValue(value);
    }

    public void addExInfo(HSSFRow row, int column, HSSFCellStyle style, String value) {
        HSSFCell bodyInfo = row.createCell(column);
        bodyInfo.setCellStyle(style);
        bodyInfo.setCellValue(value);
    }

    public void addExInfo(HSSFRow row, int column, HSSFCellStyle style, RichTextString value) {
        HSSFCell bodyInfo = row.createCell(column);
        bodyInfo.setCellStyle(style);
        bodyInfo.setCellValue(value);
    }

    public void addExInfo(HSSFRow row, int column, HSSFCellStyle style, boolean value) {
        HSSFCell bodyInfo = row.createCell(column);
        bodyInfo.setCellStyle(style);
        bodyInfo.setCellValue(value);
    }

    public void setBorder(HSSFCellStyle style) throws Exception {
        style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        style.setBorderRight(HSSFCellStyle.BORDER_THIN);
        style.setBorderTop(HSSFCellStyle.BORDER_THIN);
    }


}
